package com.bckj.freshnow.order.trade.execution.create;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.bckj.fastboot.core.execution.Execution;
import com.bckj.fastboot.core.execution.ExecutionContext;
import com.bckj.freshnow.common.gen.IdGenerator;
import com.bckj.freshnow.order.entity.TradeItem;
import com.bckj.freshnow.order.entity.TradeMarketingInfo;
import com.bckj.freshnow.order.entity.TradeOrder;
import com.bckj.freshnow.order.entity.TradeParentOrder;
import com.bckj.freshnow.order.model.CreateTradeOrderRequest;
import com.bckj.freshnow.order.repo.*;
import com.bckj.freshnow.order.trade.fsm.TradeOrderContext;
import com.bckj.freshnow.order.trade.fsm.enums.TradeOrderStatusEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@RequiredArgsConstructor
public class CreateTradeOrderSaveExecution implements Execution {

    private final TradeParentOrderRepository tradeParentOrderRepository;
    private final TradeOrderRepository tradeOrderRepository;
    private final TradeItemRepository tradeItemRepository;
    private final TradeMarketingInfoRepository tradeMarketingInfoRepository;
    private final TradeMarketingItemRelationRepository tradeMarketingItemRelationRepository;

    @Override
    public void execute(ExecutionContext context) {
        TradeOrderContext ctx = (TradeOrderContext) context;
        TradeParentOrder parentOrder = ctx.getParentOrder();
        Assert.notNull(parentOrder, "parentOrder不能为空");
        tradeParentOrderRepository.save(parentOrder);
        for (TradeOrder tradeOrder : parentOrder.getOrderList()) {
            tradeOrder.setParentOrderNo(parentOrder.getParentOrderNo());
            setTradeOrder(ctx, tradeOrder);
            tradeOrderRepository.save(tradeOrder);
            List<TradeItem> itemList = tradeOrder.getItemList();
            for (TradeItem item : itemList) {
                item.setOrderId(tradeOrder.getId());
                tradeItemRepository.save(item);
                //保存商品维度营销信息
                saveGoodsMarketingInfo(tradeOrder, item);
            }
            //保存订单维度营销信息
            saveOrderMarketingInfo(tradeOrder);
        }
    }

    private void setTradeOrder(TradeOrderContext ctx, TradeOrder tradeOrder) {
        CreateTradeOrderRequest request = (CreateTradeOrderRequest) ctx.getRequest();
        tradeOrder.setUserId(request.getUserId());
        tradeOrder.setOrderNo(IdGenerator.generateOrderNo(""));
        tradeOrder.setOrderType(ctx.getOrderType());
        if (request.getPayStatus() != null) {
            tradeOrder.setPayStatus(request.getPayStatus());
            tradeOrder.setPayTime(LocalDateTime.now());
        }
        if (request.getOrderStatus() != null) {
            tradeOrder.setOrderStatus(request.getOrderStatus());
        } else {
            tradeOrder.setOrderStatus(TradeOrderStatusEnum.PAY_ONLINE.getCode());
        }
        tradeOrder.setOrderSource(request.getOrderSource());
    }

    private void saveGoodsMarketingInfo(TradeOrder order, TradeItem item) {
        List<TradeMarketingInfo> marketingInfoList = item.getMarketingInfoList();
        if (CollUtil.isNotEmpty(marketingInfoList)) {
            for (TradeMarketingInfo info : marketingInfoList) {
                info.setOrderId(order.getId());
                info.setDiscountRange(2);
                tradeMarketingInfoRepository.save(info);
                tradeMarketingItemRelationRepository.saveRelation(info.getId(), item.getId());
            }
        }
    }

    private void saveOrderMarketingInfo(TradeOrder order) {
        List<TradeMarketingInfo> marketingInfoList = order.getMarketingInfoList();
        if (CollUtil.isNotEmpty(marketingInfoList)) {
            for (TradeMarketingInfo info : marketingInfoList) {
                info.setOrderId(order.getId());
                info.setDiscountRange(1);
                tradeMarketingInfoRepository.save(info);
                //保存参与优惠的商品关系
            }
        }
    }
}
